package com.whx.authorization.request;

import com.whx.authorization.response.BaseResponse;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * 类名：BaseRequest
 * 类路径： com.whx.authorization.request.BaseRequest
 * 创建者： whx
 * 创建时间： 10/9/18 1:49 PM
 * 项目： authorization_center
 * 描述：
 */
public abstract class BaseRequest<T extends BaseResponse> {

    protected static final String AUTHORIZATION_CENTER_SESSION_NAME = "SESSION";

    protected Map<String, String> headerMap;

    protected Map<String, String> cookieMap;

    protected Map<String, String> paramMap;

    /**
     * 请求路径
     * @return
     */
    public abstract String getMethodPath();

    public void putParam(String key, Object value) {
        putToMap(this.getParamMap(), key, value);
    }

    public void putHeaderParam(String key, String value) {
        putToMap(this.getHeaderMap(), key, value);
    }

    public void putCookieParam(String key, String value) {
        putToMap(this.getCookieMap(), key, value);
    }

    public Connection.Method getHttpMethod() {
        return Connection.Method.POST;
    }

    public abstract Class<T> getResponseClass();

    public void prepareRequest() {
        addHeaders();
        addCookies();
        addParams();
    }

    protected abstract void addParams();

    protected abstract void addCookies();

    protected abstract void addHeaders();

    public Map<String, String> getHeaderMap() {
        if (headerMap == null) {
            this.headerMap = new HashMap<String, String>();
        }
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public Map<String, String> getCookieMap() {
        if (cookieMap == null) {
            this.cookieMap = new HashMap<String, String>();
        }
        return cookieMap;
    }

    public void setCookieMap(Map<String, String> cookieMap) {
        this.cookieMap = cookieMap;
    }

    public Map<String, String> getParamMap() {
        if (paramMap == null) {
            this.paramMap = new HashMap<String, String>();
        }
        return paramMap;
    }

    public void setParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
    }



    protected String putToMap(Map<String, String> map, String key, String value) {
        return StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value) ? map.put(key, value) : null;
    }

    protected String putToMap(Map<String, String> map, String key, Object value) {
        String strValue;
        if (value == null) {
            strValue = null;
        } else if (value instanceof Date) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            strValue = format.format(value);
        } else {
            strValue = value.toString();
        }
        return putToMap(map, key, strValue);
    }
}
